﻿using System;
using System.Data;
using System.Data.Sql;
using System.Windows.Forms;

namespace APdal
{
    public partial class frmSql : Form
    {
        public frmSql()
        {
            InitializeComponent();
            cbDB.GotFocus += cbDbGotFocus;
        }

        private void frmSql_Load(object sender, EventArgs e)
        {
            getSqlServers();
        }

        private void btConnect_Click(object sender, EventArgs e)
        {
            string db;
            if (chkLocal.Checked)
            {
                db = string.Format("data source={0}; Initial Catalog={1}; integrated security=sspi; connection timeout=7;",
                    cbServers.Text, cbDB.SelectedItem);
                Properties.Settings.Default.appConStr = db;
            }
            else if ((!string.IsNullOrEmpty(txtUser.Text)) && (!string.IsNullOrEmpty(txtPass.Text)))
            {
                db = string.Format("data source={0}; Initial Catalog={1}; user id={2}; password={3}; connection timeout=7",
                     cbServers.Text, cbDB.SelectedItem, txtUser.Text, txtPass.Text);
                Properties.Settings.Default.appConStr = db;
            }
            Properties.Settings.Default.Save();

            dataLink.conn.ConnectionString = Properties.Settings.Default.appConStr;
            Close();
        }

        #region "Get Sql Servers"

        private void getSqlServers()
        {
            cbServers.Items.Clear();
            DataTable dt = SqlDataSourceEnumerator.Instance.GetDataSources();
            foreach (DataRow dr in dt.Rows)
            {
                var instanceName = dr["InstanceName"].ToString();
                if (!string.IsNullOrEmpty(instanceName))
                    cbServers.Items.Add(dr[0] + "\\" + instanceName);               
                else
                cbServers.Items.Add(dr[0].ToString());
            }
        }

        #endregion

        protected void cbDbGotFocus(object sender, EventArgs e)
        {
            try
            {
                dataLink.conn.Close();
                if (chkLocal.Checked)
                    Properties.Settings.Default.appConStr = "Data Source=" + cbServers.Text + ";Integrated Security=SSPI; Connection Timeout=5";
                else if ((!string.IsNullOrEmpty(txtUser.Text)) && (!string.IsNullOrEmpty(txtPass.Text)))
                    Properties.Settings.Default.appConStr = "Data Source=" + cbServers.Text + "; User ID=" + txtUser.Text + "; Password=" + txtPass.Text + ";Connection Timeout=5";
                dataLink.conn.ConnectionString = Properties.Settings.Default.appConStr;

                getDatabases();
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        #region "Get Databases"

        protected void getDatabases()
        {
            try
            {
                dataLink.conn.Open();
                DataTable tblDatabases = dataLink.conn.GetSchema("Databases");
                foreach (DataRow row in tblDatabases.Rows)
                    if (row["database_name"].ToString().IndexOf("AP") != -1)
                        cbDB.Items.Add(row["database_name"].ToString());
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
            dataLink.conn.Close();
        }
        #endregion

        private void btnCancelar_Click(object sender, EventArgs e)
        { Application.Exit(); }

        private void cbDbSelectedIndexChanged(object sender, EventArgs e)
        { if (cbDB.SelectedIndex != -1)   btConnect.Enabled = true; }

        private void chkLocal_CheckedChanged(object sender, EventArgs e)
        {
            txtUser.Enabled = !chkLocal.Checked;
            txtPass.Enabled = !chkLocal.Checked;
        }
    }
}

